﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BIZ;
using DAL;

namespace SinhNhat.admin
{
    public partial class EditArticle : BasePage
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
            _FileBrowser.BasePath = "/ckfinder/";
            _FileBrowser.SetupCKEditor(txtContent);

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblHeader.Text = "Create article";
                if (Request.Params["articleID"] != null)
                {
                    Guid articleID = WebUtils.ToGuid(Request.Params["articleID"]);
                    if (!articleID.Equals(Guid.Empty))
                    {
                        ArticleBIZ articleBIZ = new ArticleBIZ();
                        ARTICLE article = articleBIZ.getId(articleID);

                        if (article == null)
                        {
                            Response.Write("<script type='text/javascript'>alert('ArticleID is not correct');window.location = '/admin/AdminArticle.aspx';</script>");
                        }
                        else
                        {
                            txtTitle.Text = article.Title;
                            //txtURL.Text = WebUtils.EditURL(article.Url);
                            txtContent.Text = article.Content;
                            hdArticleID.Value = articleID.ToString();
                            hdUrlOdl.Value = article.Url;

                            this.Page.Header.Title = "Edit | " + article.Title;
                            lblHeader.Text = "Edit article";
                        }
                    }
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            ACCOUNT account = (ACCOUNT)Session["account"];
            if (account == null)
            {
                Response.Redirect("/Login.aspx");
            }

            UrlBIZ.GetAllUrl();
            string url = WebUtils.CorrectURL(txtTitle.Text);

            ArticleBIZ articleBIZ = new ArticleBIZ();
            ARTICLE article = new ARTICLE();

            article.Title = txtTitle.Text;
            article.Url = url;
            article.Content = txtContent.Text;

            if (hdArticleID.Value.Equals(string.Empty))
            {

                //check url
                if (UrlBIZ.CheckPathExist(url) != PathType.PhysicalPath)
                {
                    lblError.Visible = true;
                    return;
                }

                article.AccountID = account.AccountID;
                article.IsLock = false;

                articleBIZ.insert(article);
            }
            else
            {
                //check url
                if (!hdUrlOdl.Value.Equals(url))
                {
                    if (UrlBIZ.CheckPathExist(url) != PathType.PhysicalPath)
                    {
                        lblError.Visible = true;
                        return;
                    }
                }

                Guid articleID = WebUtils.ToGuid(hdArticleID.Value);
                articleBIZ.edit(article, articleID);
            }

            Response.Redirect("/admin/AdminArticle.aspx");
        }
    }
}